#include "Koopa.h"
//#include "Global.h"
#include "Resource.h"

Koopa::Koopa(float x, float y)
{
	check_turn = false;
 
	_x = x;
	_y = y;
	_vx = (float)KOOPA_VX;
	_vy = 0;
}

Koopa::Koopa(LPDIRECT3DDEVICE9 d3ddv,  LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, int Object_State, int Direction)
{
	check_turn = false;

	_x = x;
	_y = y;
	_vx = -(float)KOOPA_VX;

	_vy = 0;
	_accelY = 0;
	_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\turtle.png"), 100, 0);

	 

	timeStop = 0;

	collisionObjectList = new list<Object*>;

	//object_State = ES_ACTIVING;
	object_State = Object_State;
	direction = Direction;
	if (object_State == ES_ACTIVING)
	{
		if (direction == 1)			//sang trai
		{
			_vx = -(float)KOOPA_VX;
		} 
		else if (direction == 2)	//sang phai
		{
			_vx = (float)KOOPA_VX;
		}
	}
	else if (object_State == ES_STOP)
	{
		timeStop = 0;
		_vx = 0;
		_sprite->SetFrame(6,7);
		
	}
	else if (object_State == ES_MOVE_SHELL_LEFT)
	{
		_sprite->SetFrame(6,8);

		_vx = -KOOPA_VX * 10;
		_accelY = GRAVITY;

		 
	}
	else if (object_State == ES_MOVE_SHELL_RIGHT)
	{
		_sprite->SetFrame(6,8);

		_vx = KOOPA_VX * 10;
		_accelY = GRAVITY;


	}
}

Koopa::~Koopa()
{
	/*if (_sprite != NULL)
	delete _sprite;*/
	if (collisionObjectList){
		delete collisionObjectList;
		//collisionObjectList = NULL;
	}
}




void Koopa::Render()
{

	if (object_State != ES_REMOVE)
	{

		//if (object_State == ES_STOP || object_State == ES_MOVE_SHELL_LEFT || object_State == ES_MOVE_SHELL_RIGHT)
		//{
		//	//_spriteShell->Render((int)_x,(int)_y);
		//}
		//else
		//{
		//	
		//}
		_sprite->Render((int)_x,(int)_y);
	}

}

OBJECT_TYPE Koopa::GetTypeObject()
{
	return OBJECT_ENEMY;
}

/**--------------------------------------------------------
*Description : Turn left Turtle
*Method name : TurnLeft
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void Koopa :: TurnLeft()
{
	check_turn = true;
	 

	if (object_State == ES_MOVE_SHELL_RIGHT || object_State == ES_MOVE_SHELL_LEFT)
	{
		 
		_sprite->SetFrame(6, 9);
	}
	else	//state active
	{
		_sprite->SetFrame(0, 2);
		 
	}
}


/**--------------------------------------------------------
*Description : Turn right Turtle
*Method name : TurnRight
*Parameters :   
*Return value : void
--------------------------------------------------------**/
void Koopa :: TurnRight()
{
	check_turn = false;
	 

	if (object_State == ES_MOVE_SHELL_RIGHT || object_State == ES_MOVE_SHELL_LEFT)
	{
		 
		_sprite->SetFrame(6, 9);
	}
	else	//state active
	{
		_sprite->SetFrame(2, 4);
		 
	}
}

void Koopa::Update(list<Object*>* staObjs, list<Object*>* dynObjs, DxInput* input, float gameTime)
{
	list<Object*>::iterator i;
	Object* obj;
	switch (object_State)
	{
	case ES_ACTIVING:
		Move(staObjs, gameTime);

		if (_y > 600)	//screen height: 600
		{
			ChangeState(ES_REMOVE);
		}
		_sprite->Update(gameTime);
		break;
	case ES_MOVE_SHELL_LEFT:
	case ES_MOVE_SHELL_RIGHT:
		Move(staObjs, gameTime);
		for (i = dynObjs->begin(); i != dynObjs->end(); i++)
		{
			obj = *i;
			if (obj->GetID() == 11 || (obj->GetID() == 12 && obj != this))
			{
				if (Collision::RectVsRect(this->GetBond(), obj->GetBond()) && obj->object_State == ES_ACTIVING)
				{
					obj->ChangeState(ES_FALL);
				}
			}
		}
		if (_y > 600)	//screen height: 600
		{
			ChangeState(ES_REMOVE);
		}
		_sprite->Update(gameTime);
		//_spriteShell->Update(gameTime);
		ResetRect();
		break;
	case ES_STOP:
		timeStop += gameTime;
		if (timeStop > 2900)
		{
			timeStop -= 2900;
			ChangeState(ES_ACTIVING);
		}
		//_spriteShell->Update(gameTime);
		_sprite->Update(gameTime);
		ResetRect();
		break;
	case ES_FALL:
		_x += _vx * gameTime;
		_vy += _accelY * gameTime;
		_y += _vy * gameTime + 1/2 * _accelY * gameTime *gameTime;
		if (_y > 600)
		{
			ChangeState(ES_REMOVE);
		}
		//ResetRect();
		ResetRectNULL();
		_sprite->Update(gameTime);

		break;
	case ES_FALLBULLET:
		_x += _vx * gameTime;
		_vy += _accelY * gameTime;
		_y += _vy * gameTime + 1/2 * _accelY * gameTime *gameTime;
		if (_y > 600)
		{
			ChangeState(ES_REMOVE);
		}
		//ResetRect();
		_sprite->Update(gameTime);

		break;
	}


}

void Koopa::Move(list<Object*>* staObjs, float gameTime)
{
	list<Object*>::iterator i;
	Object* obj;

	collisionObjectList->clear();
	for (i = staObjs->begin(); i != staObjs->end(); i++)
	{
		obj = *i;
		if (obj->GetTypeObject() == OBJECT_LAND)
		{
			collisionObjectList->push_back(obj);
		}
	}

	//Xet va cham theo truc X
	_x += _vx * gameTime;
	ResetRect();

	for (i = staObjs->begin(); i != staObjs->end(); i++)
	{
		obj = *i;
		if (Collision::RectVsRect(this->GetBond(), obj->GetBond()))
		{
			//xet va cham gach, da, ong,...
			//if (obj->GetID() >= 21 && obj->GetID() <= 24)
			if (obj->GetTypeObject() == OBJECT_LAND)
			{
				_vx = -_vx;
				_x = _xOld;
			}
		}
		ResetRect();
	}
	if(_vx > 0)
	{
		TurnRight();
	}
	else
	{
		TurnLeft();
	}

	//Xet va cham theo truc Y
	_accelY = GRAVITY;
	_vy += _accelY * gameTime;
	_y += _vy * gameTime + 1/2.0 * _accelY * gameTime * gameTime;
	

	rectOld = _rectBond;
	ResetRect();


	//kiem tra va cham nam tren gach, da
	if(collisionObjectList->size() > 0)
	{
		Object* obj;
		list<Object*>::iterator i;
		for(i = collisionObjectList->begin(); i != collisionObjectList->end(); i++)
		{
			obj = *i;
			//kiem tra va cham nam tren gach, da,... khong cho roi xuong
			if(this->GetBond().bottom <= obj->GetBond().bottom && this->GetBond().left < obj->GetBond().right && this->GetBond().right > obj->GetBond().left)
			//if(rectOld.bottom<obj->GetBond().bottom)
				if(obj->GetY() - this->_sprite->texture->_Height < this->_y)
				{
					_vy = 0;
					_y = obj->GetY() - this->GetSprite()->texture->_Height;
				}

		}
		ResetRect();
	}


	_xOld = _x;
	_yOld = _y;

	if (_vx >= 0)	//sang phai
	{
		direction = 2;
	} 
	else
	{
		direction = 1;
	}
}

void Koopa::ChangeState(int state)
{
	object_State = state;
	switch (object_State)
	{
	case ES_ACTIVING:
		if (check_turn == false)	 //turn right
		{
			_vx = KOOPA_VX;
		} 
		else						//turn left
		{
			_vx = -KOOPA_VX;
		}
		 
		_accelY = GRAVITY;
		break;

	case ES_FALL:
		if (check_turn == false)	 //turn right
		{
			_sprite->SetFrame(5,6);
		} 
		else						//turn left
		{
			_sprite->SetFrame(4,5);
		}
		_vy = -1.5;
		_accelY = GRAVITY;
		 
		break;
	case ES_FALLBULLET:
		if (check_turn == false)	 //turn right
		{
			_sprite->SetFrame(10,10);
		} 
		else						//turn left
		{
			_sprite->SetFrame(9,9);
		}
		//_sprite->SetFrame(9,9);
		_vy = -1.5;
		_accelY = GRAVITY;

		break;

	case ES_MOVE_SHELL_LEFT:
		_sprite->SetFrame(6,8);
		 
		_vx = -KOOPA_VX * 10;
		_accelY = GRAVITY;
		break;
	case ES_MOVE_SHELL_RIGHT:
		_sprite->SetFrame(6,8);
		 
		_vx = KOOPA_VX * 10;
		_accelY = GRAVITY;
		break;

	case ES_STOP:
		timeStop = 0;
		_vx = 0;
		 
		_sprite->SetFrame(6,7);
		break;
	}
}

void Koopa::ResetRect()
{
	//dang them vao
	_rectBond.left = _x;
	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
	_rectBond.top = _y;
	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;

}

void Koopa::ResetRectNULL()
{
	//dang them vao
	_rectBond.left = 0;
	_rectBond.right = _rectBond.left + 0;
	_rectBond.top = 0;
	_rectBond.bottom = _rectBond.top + 0;

}